package cn.iocoder.yudao.module.iot.gateway.protocol.tcp.detector;

import io.vertx.core.net.NetSocket;

import java.util.Collections;
import java.util.Set;

public class ContentPatternDetectionStrategy implements ProtocolDetectionStrategy {
    private final Set<String> supportedProtocols;

    public ContentPatternDetectionStrategy() {
        // 初始化支持的协议类型
        supportedProtocols = Collections.singleton("content-pattern");
    }

    @Override
    public String detectProtocol(byte[] data, NetSocket socket) {
        if (data == null || data.length == 0) {
            return null;
        }

        String content = new String(data);
        if (content.contains("\"method\"") && content.contains("\"params\"")) {
            return "json"; // JSON格式协议
        }

        // 简单的二进制协议检测
        if (isValidBinaryProtocol(data)) {
            return "binary";
        }
        return null;
    }

    private boolean isValidBinaryProtocol(byte[] data) {
        // 简单示例：检查是否为有效的二进制格式
        // 实际实现需要根据具体协议规范来判断
        return data.length > 4 && data[0] != '{' && data[0] != '[';
    }

    @Override
    public int getPriority() {
        return 15;
    }

    @Override
    public Set<String> getSupportedProtocols() {
        return supportedProtocols;
    }
}
